package com.LFLWZ.controller;


import com.LFLWZ.ServiceImpl.worksServiceImpl;
import com.LFLWZ.beans.Dto.Dto;
import com.LFLWZ.beans.Dto.Page;
import com.LFLWZ.beans.POJO.*;
import com.LFLWZ.beans.Vo.ClassroomCourseVo;
import com.LFLWZ.beans.Vo.CommentsVo;
import com.LFLWZ.beans.Vo.LecturerAndCloudLogin;
import com.LFLWZ.service.ShowPageService;
import com.LFLWZ.utils.DtoUtil;
import com.LFLWZ.utils.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("works")
public class worksController {

    @Autowired
    private worksServiceImpl worksServiceImpl;

    @GetMapping("auditWorks/{audit}/{type}")
    public Dto auditWorks(@PathVariable Integer audit,@PathVariable Integer type) {
        Dto dto = null;
        System.out.println("sd");
        if (EmptyUtils.isEmpty(audit)) {
            dto = DtoUtil.returnError(4001, "参数为空");
        }
        Worksshow worksshow = new Worksshow();
        worksshow.setAudit(audit);
        worksshow.setSpecial(type);
        System.out.println(worksshow.getAudit());
        List<Worksshow> list = worksServiceImpl.list(worksshow);
        if (EmptyUtils.isEmpty(list)) {
            dto = DtoUtil.returnError(4002, "查询为空");
        } else {
            dto = DtoUtil.returnSuccessData(list);
        }
        return dto;
    }

    /**
     *查询所有用户
     * @param
     * @return
     */
    @GetMapping("loginsel")
    public Dto loginsel( CloudLogin cloudLogin) {
        Dto dto = null;
        List<CloudLogin> list = worksServiceImpl.loginsel(cloudLogin);
        if (EmptyUtils.isEmpty(list)) {
            dto = DtoUtil.returnError(4001, "数据错误");
        } else {
            dto = DtoUtil.returnSuccessData(list);
        }
        return dto;
    }
    @PostMapping("updlogin")
    public Dto updlogin(@RequestBody CloudLogin cloudLogin){
        Dto dto = null;
        if (EmptyUtils.isEmpty(cloudLogin.getUid()) && EmptyUtils.isEmpty(cloudLogin.getNickname()))
            dto = DtoUtil.returnError(4001, "参数不能是空");
        int line = worksServiceImpl.updlogin(cloudLogin);
        if (line > 0) {
            dto = DtoUtil.returnSuccessData(line);
        } else {
            dto = DtoUtil.returnError(4001, "修改失败");
        }
        return dto;
    }

    /**
     * 查询所有课程评论
     * @param commentsVo
     * @return
     */
    @PostMapping("selLesson")
    public Page<CommentsVo> queryLesson(@RequestBody CommentsVo commentsVo){
        return worksServiceImpl.queryLesson(commentsVo);
    }

    /**
     * 查询所有章节评论
     * @param commentsVo
     * @return
     */
    @PostMapping("selSection")
    public Page<CommentsVo> querySectiom(@RequestBody CommentsVo commentsVo){
        return worksServiceImpl.querySection(commentsVo);
    }
    /**
     * 查询所有日记和作品评论
     * @param commentsVo
     * @return
     */
    @PostMapping("selDW")
    public Page<CommentsVo> queryDW(@RequestBody CommentsVo commentsVo){
        return worksServiceImpl.queryDW(commentsVo);
    }
    @PostMapping("update")
    public Dto update(@RequestBody Worksshow worksshow) {
        Dto dto = null;
        if (EmptyUtils.isEmpty(worksshow.getId()) && EmptyUtils.isEmpty(worksshow.getAudit()))
            dto = DtoUtil.returnError(4001, "参数不能为空");
        int line = worksServiceImpl.update(worksshow);
        if (line > 0) {
            dto = DtoUtil.returnSuccessData(line);
        } else {
            dto = DtoUtil.returnError(4001, "修改失败");
        }
        return dto;
    }

    @GetMapping("auditLecturer")
    public Dto auditLecturer(LecturerApply lecturerApply) {
        Dto dto = null;
        if (EmptyUtils.isEmpty(lecturerApply)) {
            dto = DtoUtil.returnError(4001, "参数为空");
        }
        List<LecturerApply> list = worksServiceImpl.selLecturer(lecturerApply);
        if (EmptyUtils.isEmpty(list)) {
            dto = DtoUtil.returnError(4002, "查询为空");
        } else {
            dto = DtoUtil.returnSuccessData(list);
        }
        return dto;
    }

    @PostMapping("updAudit")
    public Dto updAudit(@RequestBody LecturerApply lecturerApply) {
        Dto dto = null;
        if (EmptyUtils.isEmpty(lecturerApply.getId()) && EmptyUtils.isEmpty(lecturerApply.getAudit()))
            dto = DtoUtil.returnError(4001, "参数不能是空");
        int line = worksServiceImpl.updAudit(lecturerApply);
        if (line > 0) {
            dto = DtoUtil.returnSuccessData(line);
        } else {
            dto = DtoUtil.returnError(4001, "修改失败");
        }
        return dto;
    }

    @GetMapping("slideshow")

    public Dto slideshow() {
        return DtoUtil.returnSuccessData(worksServiceImpl.queryrotation(0, 10));
    }

    @GetMapping("menu")

    public Dto menu() {
        return DtoUtil.returnSuccessData(worksServiceImpl.querymenu(0, 10));
    }

    @GetMapping("list")

    public Dto list() {
        return DtoUtil.returnSuccessData(worksServiceImpl.querylist(0, 10));
    }

    @PostMapping("updater")
    public Dto updater(@RequestBody ShowPage showpage) {
        Dto dto = null;
        if (EmptyUtils.isEmpty(showpage.getId()))
            dto = DtoUtil.returnError(4001, "参数不能是空");
        int line = worksServiceImpl.updater(showpage);
        if (line > 0) {
            dto = DtoUtil.returnSuccessData(line);
        } else {
            dto = DtoUtil.returnError(4001, "修改失败");
        }
        return dto;
    }

    @PostMapping("updStatus")
    public Dto updStuatus(@RequestBody ShowPage showPage) {
        Dto dto = null;
        if (EmptyUtils.isEmpty(showPage.getId()) && EmptyUtils.isEmpty(showPage.getStatus()))
            dto = DtoUtil.returnError(4001, "参数不能是空");
        int line = worksServiceImpl.updStatus(showPage);
        if (line > 0) {
            dto = DtoUtil.returnSuccessData(line);
        } else {
            dto = DtoUtil.returnError(4001, "修改失败");
        }
        return dto;
    }

    @PostMapping("inst")
    public Dto inst(@RequestBody ShowPage showPage) {
        Dto dto = null;
        if (EmptyUtils.isEmpty(showPage.getImageurl()) && EmptyUtils.isEmpty(showPage.getStatus())) {
            dto = DtoUtil.returnError(4001, "参数不能为空");
        }
        int line = worksServiceImpl.inst(showPage);
        if (line > 0) {
            dto = DtoUtil.returnSuccessData(line);
        } else {
            dto = DtoUtil.returnError(4001, "修改失败");
        }
        return dto;
    }

    @PostMapping("delt")
    public Dto delt(@RequestBody ShowPage showPage) {
        Dto dto = null;
        if (EmptyUtils.isEmpty(showPage.getId())) {
            dto = DtoUtil.returnError(4001, "参数不能为空");
        }
        int line = worksServiceImpl.delt(showPage.getId());
        if (line > 0) {
            dto = DtoUtil.returnSuccessData(line);
        } else {
            dto = DtoUtil.returnError(4001, "修改失败");
        }
        return dto;
    }

    @PostMapping("upda")
    public Dto upda(@RequestBody ShowPage showPage) {
        Dto dto = null;
        if (EmptyUtils.isEmpty(showPage)) {
            dto = DtoUtil.returnError(4001, "参数不能为空");
        }
        int line = worksServiceImpl.upda(showPage);
        if (line > 0) {
            dto = DtoUtil.returnSuccessData(line);
        } else {
            dto = DtoUtil.returnError(4001, "修改失败");
        }
        return dto;
    }
    @GetMapping("selall")
    public Dto selall(Lecturer lecturer){
        Dto dto  = null;
        List<Lecturer> list = worksServiceImpl.selall(lecturer);
        if(EmptyUtils.isEmpty(list)){
            dto = DtoUtil.returnError(4001,"数据错误");
        }else{
            dto = DtoUtil.returnSuccessData(list);
        }
        return dto;
    }
    @GetMapping("selexc")
    public Dto selexc(Integer excellent){
        System.out.println(excellent);
        Dto dto = null;
        if(excellent==null||excellent==0){
            excellent = null;
        }
        List<Lecturer> list = worksServiceImpl.selexc(excellent);
        return DtoUtil.returnSuccessData(list);
    }

    /***
     * 查询登陆表与讲师表(讲师管理)
     * @param
     * @param
     * @return
     */
    @GetMapping("/Lsel")
    public Dto Lsel(LecturerAndCloudLogin lecturerAndCloudLogin){
        Dto dto = null;
        if(lecturerAndCloudLogin.getExcellent()==null||lecturerAndCloudLogin.getExcellent()==0) {
            lecturerAndCloudLogin.setExcellent(null);
        }
        List<LecturerAndCloudLogin> list = worksServiceImpl.Lsel(lecturerAndCloudLogin);
        for(LecturerAndCloudLogin le:list){
            ClassroomCourseVo classroomCourseVo=new ClassroomCourseVo();
            classroomCourseVo.setCourseauthorid(le.getId());
            le.setClassroomCoursesList(worksServiceImpl.selClass(classroomCourseVo));
        }
        if (EmptyUtils.isEmpty(list)){
            dto = DtoUtil.returnError(4001,"没有该讲师");
        }else{
            dto = DtoUtil.returnSuccessData(list);
        }
        return dto;
    }
    /***
     * 修改讲师数据(讲师管理)
     * @param lecturer
     * @return
     */
    @PostMapping("/upData")
    public Dto upData(@RequestBody Lecturer lecturer){
        Dto dto = null;
        int line = worksServiceImpl.upData(lecturer);
        if(line < 0){
            dto = DtoUtil.returnError(4001,"修改失败");
        }else{
            dto = DtoUtil.returnSuccessData(line);
        }
        return dto;
    }

    /***
     * 根据ID删除讲师数据(讲师管理)
     * @param id
     * @return
     */
    @PostMapping("/del")
    public Dto del(Integer id){
        Dto dto =null;
        int line = worksServiceImpl.del(id);
        if(line<0){
            dto = DtoUtil.returnError(4001,"删除失败");
        }else{
            dto = DtoUtil.returnSuccessData(line);
        }
        return dto;
    }

    /***
     * 查询专栏(课程专栏)
     * @param column
     * @return
     */
    @PostMapping("/selCol")
    public Dto selCol(Column column){
        Dto dto = null;
        if(column.getStatus() == 0){
            column.setStatus(null);
        }
        List<Column> list = worksServiceImpl.selCol(column);
        if(EmptyUtils.isEmpty(list)){
            dto = DtoUtil.returnError(4001,"查询失败");
        }else{
            dto = DtoUtil.returnSuccessData(list);
        }
        return  dto;
    }

    /***
     * 添加专栏数据(课程专栏)
     * @param column
     * @return
     */
    @PostMapping("/insCol")
    public Dto insCol(@RequestBody Column column){
        Dto dto = null;
        int line = worksServiceImpl.insCol(column);
        if(line < 0){
            dto = DtoUtil.returnError(4001,"添加失败");
        }else{
            dto = DtoUtil.returnSuccessData(line);
        }
        return dto;
    }

    /***
     * 修改专栏数据(课程专栏)
     * @param column
     * @return
     */
    @PostMapping("/upCol")
    public Dto upCol(@RequestBody Column column){
        Dto dto = null;
        int line = worksServiceImpl.upCol(column);
        if(line < 0){
            dto = DtoUtil.returnError(4001,"修改失败");
        }else{
            dto = DtoUtil.returnSuccessData(line);
        }
        return dto;
    }

    /***
     * 删除专栏数据(课程专栏)
     * @param id
     * @return
     */
    @PostMapping("/delCol")
    public Dto delCol(Integer id){
        Dto dto = null;
        int line = worksServiceImpl.delCol(id);
        if(line < 0){
            dto = DtoUtil.returnError(4001,"删除失败");
        }else{
            dto = DtoUtil.returnSuccessData(line);
        }
        return  dto;
    }

    @PostMapping("/selClass")
    public Dto selClass(LecturerAndCloudLogin lecturerAndCloudLogin){
        Dto dto = null;
        ClassroomCourseVo classroomCourseVo = new ClassroomCourseVo();
        classroomCourseVo.setCourseauthorid(lecturerAndCloudLogin.getId());
        List<ClassroomCourseVo> list = worksServiceImpl.selClass(classroomCourseVo);
        if(EmptyUtils.isEmpty(list)){
            dto  = DtoUtil.returnError(4001,"查询失败");
        }else{
            dto = DtoUtil.returnSuccessData(list);
        }
        return dto;
    }
    @PostMapping("/insLect")
    public Dto insLect(@RequestBody LecturerAndCloudLogin lecturerAndCloudLogin){
        Dto dto = null;
        int line = 0;
        List<LecturerAndCloudLogin> list = worksServiceImpl.Lsel(lecturerAndCloudLogin);
        int random = (int)((Math.random()*9+1)*100000);
        CloudLogin cloudLogin = new CloudLogin();
        Lecturer lecturer = new Lecturer();
        lecturer.setName(lecturerAndCloudLogin.getName());
        lecturer.setId(lecturerAndCloudLogin.getId());
        lecturer.setLecturer_Img(lecturerAndCloudLogin.getLecturer_Img());
        lecturer.setBackground_Img(lecturerAndCloudLogin.getBackground_Img());
        lecturer.setExcellent(lecturerAndCloudLogin.getExcellent());
        lecturer.setIntro(lecturerAndCloudLogin.getIntro());
        lecturer.setUid(random);
        line = worksServiceImpl.inslecturer(lecturer);
        cloudLogin.setGender(lecturerAndCloudLogin.getGender());
        cloudLogin.setPhone(lecturerAndCloudLogin.getPhone());
        cloudLogin.setPassword(lecturerAndCloudLogin.getPassword());
        cloudLogin.setQq(lecturerAndCloudLogin.getQq());
        cloudLogin.setUid(random);
        line = worksServiceImpl.insLogin(cloudLogin);
        System.out.println(random);
        if(line <0){
            dto = DtoUtil.returnError(4001,"添加失败");
        }else{
            dto =DtoUtil.returnSuccessData(line);
        }
        return dto;
    }
    @PostMapping("/inslink")
    public Dto inslink(@RequestBody LecturerAndCloudLogin lecturerAndCloudLogin){
        Dto dto = null;
        int line;
        Lecturer lecturer = new Lecturer();
        CloudLogin cloudLogin = new CloudLogin();
        lecturer.setName(lecturerAndCloudLogin.getName());
        lecturer.setLecturer_Img(lecturerAndCloudLogin.getLecturer_Img());
        lecturer.setExcellent(lecturerAndCloudLogin.getExcellent());
        lecturer.setIntro(lecturerAndCloudLogin.getIntro());
        lecturer.setUid(lecturerAndCloudLogin.getUid());
        lecturer.setBackground_Img(lecturerAndCloudLogin.getBackground_Img());
        line= worksServiceImpl.inslecturer(lecturer);
        dto = DtoUtil.returnSuccessData(line);
        return dto;
    }
    @PostMapping("/selUid")
    public Dto selUid(@RequestBody CloudLogin cloudLogin){
        Dto dto = null;
        List<CloudLogin> list = worksServiceImpl.selUid(cloudLogin);
        if(EmptyUtils.isEmpty(list)){
            dto = DtoUtil.returnError(4001,"查询失败");
        }else{
            dto = DtoUtil.returnSuccessData(list);
        }
        return dto;
    }
    @PostMapping("/selLeUid")
    public Dto selLeUid(@RequestBody Lecturer lecturer){
        Dto dto = null;
        List<Lecturer> list = worksServiceImpl.selLeUid(lecturer);
        if(EmptyUtils.isEmpty(list)){
            dto = DtoUtil.returnError(4001,"查询失败");
        }else{
            dto = DtoUtil.returnSuccessData(list);
        }
        return dto;
    }
    @PostMapping("/selById")
    public Dto selBy(@RequestBody Lecturer lecturer){
        Dto dto = null;
        List<Lecturer> list = worksServiceImpl.selBy(lecturer);
        if(EmptyUtils.isEmpty(list)){
            dto = DtoUtil.returnError(4001,"查询失败");
        }else{
            dto =DtoUtil.returnSuccessData(list);
        }
        return dto;
    }
}